<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.keyWord" placeholder="数据字典名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="query">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="systems" @row-dblclick="getClick" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="sn" label="编号" width="100" >
      </el-table-column>
      <el-table-column prop="name" label="数据字典名称" min-width="180" >
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :current-page="currentPage" :page-size="pageSize" :total="total" style="float:right;">
      </el-pagination>
    </el-col>

    <!--编辑/新增界面-->
    <el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="false">
      <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
        <el-form-item label="数据字典" prop="editFormRules.name">
          <el-input v-model="editForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="部门编号" prop="editFormRules.sn">
          <el-input v-model="editForm.sn" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>

    <!--外拉框得编辑/新增界面-->
    <el-dialog :title="title" :visible.sync="editFormmVisible" :close-on-click-modal="false">
      <el-form :model="editFormmm" label-width="80px" :rules="editFormRules" ref="editFormmm">
        <el-form-item label="名称" prop="editFormRules.name">
          <el-input v-model="editFormmm.name" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormmVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmittt" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>

    <el-drawer
        title="详情"
        :visible.sync="table"
        direction="rtl"
        size="50%">


      <!--外拉框得表单-->
      <el-table :data="system" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column type="index" width="60">
        </el-table-column>
        <el-table-column prop="name" label="名称" min-width="180" >
        </el-table-column>
        <el-table-column prop="systemType.name" label="属于" width="100" >
        </el-table-column>
        <el-table-column label="操作" width="150">
          <template slot="header" slot-scope="scope">
            <el-button size="small" @click="handleAddd(scope.$index, scope.row)">新增</el-button>
          </template>
          <template scope="scope">
            <el-button size="small" @click="handleEditqq(scope.$index, scope.row)">编辑</el-button>
            <el-button type="danger" size="small" @click="handleDeeel(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--工具条-->
      <el-col :span="24" class="toolbar">
        <el-button type="danger" @click="batchRemoveee" :disabled="this.sels.length===0">批量删除</el-button>
        <el-pagination layout="prev, pager, next" @current-change="handleCurrentChangeee" :current-page="currentPagee" :page-size="pageSizee" :total="totals" style="float:right;">
        </el-pagination>
      </el-col>
    </el-drawer>

  </section>
</template>


<script>
import util from '../../common/js/util'
//import NProgress from 'nprogress'
import { getSystemListPage, removeSystem, batchRemoveSystem, editSystem, addSystem } from '../../api/api';

export default {
  data() {
    return {
      rowww:[],
      table:false,
      gridData:[],
      options:[],
      filters: {
        keyWord: ''
      },
      filterss: {
        keyWord: ''
      },
      systems: [],
      system: [],
      total: 0,
      totals: 0,
      pageSize:5,
      currentPage:1,
      pageSizee:5,
      currentPagee:1,
      listLoading: false,
      sels: [],//列表选中列
      title:"",
      editFormVisible: false,//编辑界面是否显示
      editFormmVisible:false,
      editLoading: false,
      editFormRules: {
        name: [
          { required: true, message: '请输入数据字典名称', trigger: 'blur' }
        ],
        sn: [
          { required: true, message: '请输入编号', trigger: 'blur' }
        ],
        manager: [
          { required: false, message: '请输入负责人', trigger: 'blur' }
        ],
        parent: [
          { required: false, message: '请输入上级部门', trigger: 'blur' }
        ]
      },
      //编辑界面数据
      editForm: {
        id:null,
        sn:"",
        name:""
      },
      editFormmm:{
        id:null,
        types_id:null,
        name:"",
        systemType:{}
      },
      emp:[]
    }
  },
  methods: {
    getaa(row){
      let para = {
        currentPage: this.currentPagee,
        pageSize: this.pageSize,
        keyWord: this.filters.keyWord,
        types_id: row.id
      };
      console.log(this.currentPagee)
      this.listLoading = true;
      this.$http.post("/type",para)
          .then(result=>{
            this.listLoading = false;
            this.system = result.data.rows;
            this.totals = result.data.total;
          })
          .catch(result=>{
            this.$message({
              message: '系统错误',
              type: 'error'
            });
          })
      console.log("2222" + this.totals)
    },


    getClick(row){
      this.table = true;
      this.rowww = row;
      if(this.currentPagee = 2){
        this.currentPagee = 1;
      }
     this.getaa(row);

     },
    getparent(){
      this.$http.get("/system/tree")
          .then(result=>{
            this.options = result.data;
          })
    },
    getemp(){
      this.$http.get("/employee")
          .then(result=>{
            this.emp = result.data;
            this.getSystems();
          })
          .catch(result=>{
            this.$message({
              message: result.message,
              type: 'error'
            });
          })
    },

    handleCurrentChange(val) {
      this.currentPage = val;
      this.getSystems();
    },
    handleCurrentChangeee(val) {
      this.currentPagee = val;
      this.getaa(this.rowww);
    },
    query(){
      this.currentPage = 1;
      this.getSystems();
    },

    queryy(){
      this.currentPagee = 1;
      this.getbb();
    },
    //获取部门列表
    getSystems() {
      let para = {
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        keyWord: this.filters.keyWord
      };
      this.listLoading = true;
      this.$http.post("/detail",para)
          .then(result=>{
            this.listLoading = false;
            this.systems = result.data.rows;
            this.total = result.data.total;
          })
          .catch(result=>{
            this.$message({
              message: '系统错误',
              type: 'error'
            });
          });
      console.log("2222" + this.total);
    },
    //删除
    handleDel: function (index, row) {
      this.currentPage = 1;
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        // 发送axios请求
        this.$http.delete("/detail/" + row.id)
            .then(result=>{
              this.listLoading = false;
              if (result.data.success){
                this.$message({
                  message: '操作成功',
                  type: 'success'
                });
              }else {
                this.$message({
                  message: '操作成功',
                  type: 'error'
                });
              }
              // 调用查询方法
              this.getSystems();
            })

      }).catch(() => {

      });
    },

    //外拉删除
    handleDeeel: function (index, row) {
      this.currentPagee = 1;
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        // 发送axios请求
        this.$http.delete("/type/" + row.id)
            .then(result=>{
              this.listLoading = false;
              if (result.data.success){
                this.$message({
                  message: '操作成功',
                  type: 'success'
                });
              }else {
                this.$message({
                  message: '操作成功',
                  type: 'error'
                });
              }
              // 调用查询方法
              this.getClick(this.rowww);
            })

      }).catch(() => {

      });
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      this.title="编辑";
      this.editFormVisible = true;
      this.editForm = Object.assign({}, row);
      let npa = row.dirPath.split("/")
      let parent = [];
      for (let i = 1; i < npa.length-1; i++) {
        parent.push(parseInt(npa[i]));
      }
      this.editForm.parent = parent;

    },
    //显示外拉框得编辑界面
    handleEditqq: function (index, row) {
      this.title="编辑";
      this.editFormmVisible = true;
      this.editFormmm = Object.assign({}, row);
    },
    //显示新增界面
    handleAdd: function () {
      this.title="新增";
      this.editFormVisible = true;
      this.editForm = {
        id:null,
        sn:"",
        name:""
      };
    },

    //显示新增界面
    handleAddd: function () {
      this.title="新增";
      this.editFormmVisible = true;
      this.editFormmm={
        id:null,
        types_id:"",
        name:"",
        systemType:{}
      }
    },
    //编辑
    editSubmit: function () {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.editLoading = true;
            //NProgress.start();
            let para = Object.assign({}, this.editForm);
            this.$http.put("/detail",para)
                .then(result=>{
                  this.editFormVisible = false;
                  this.editLoading = false;
                  if (result.data.success){
                    this.$message({
                      message: '操作成功',
                      type: 'success'
                    });
                  }else {
                    this.$message({
                      message: '操作成功',
                      type: 'error'
                    });
                  }
                  // 调用查询方法
                  this.getClick(row);
                })

          }).catch(() => {
            this.editFormVisible = false;
          });
        }
      });
    },

    //外拉框得编辑
    editSubmittt: function () {
      this.editFormmm.types_id =  this.editForm.id
      this.$refs.editFormmm.validate((vali) => {
        if (vali) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.editLoading = true;
            //NProgress.start();
            this.editFormmm.types_id = this.rowww.id;
            console.log("aa" + this.editFormmm.types_id)

            let para = Object.assign({}, this.editFormmm);
            console.log(this.editFormmm.types_id);
            console.log(para);
            this.$http.put("/type",para)
                .then(result=>{
                  this.editFormmVisible = false;
                  this.editLoading = false;
                  if (result.data.success){
                    this.$message({
                      message: '操作成功',
                      type: 'success'
                    });
                  }else {
                    this.$message({
                      message: '操作成功',
                      type: 'error'
                    });
                  }``
                  // 调用查询方法
                  this.getClick(this.rowww);
                })

          }).catch(() => {
            this.editFormVisible = false;
          });
        }
      });
    },

    //新增
    addSubmit: function () {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.addLoading = true;
            //NProgress.start();
            let para = Object.assign({}, this.addForm);
            para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd');
            addSystem(para).then((res) => {
              this.addLoading = false;
              //NProgress.done();
              this.$message({
                message: '提交成功',
                type: 'success'
              });
              this.$refs['addForm'].resetFields();
              this.addFormVisible = false;
              this.getSystems();
            });
          });
        }
      });
    },
    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      this.currentPage = 1;
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        // 发送axios请求
        this.$http.patch("/detail",ids)
            .then(result=>{
              this.listLoading = false;
              if (result.data.success){
                this.$message({
                  message: '操作成功',
                  type: 'success'
                });
              }else {
                this.$message({
                  message: '操作异常',
                  type: 'error'
                });
              }
              // 调用查询方法
              this.getSystems();
            })
      }).catch(() => {

      });
    },
    //外拉批量删除
    batchRemoveee: function () {
      this.currentPagee = 1;
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        // 发送axios请求
        this.$http.patch("/type",ids)
            .then(result=>{
              this.listLoading = false;
              if (result.data.success){
                this.$message({
                  message: '操作成功',
                  type: 'success'
                });
              }else {
                this.$message({
                  message: '操作异常',
                  type: 'error'
                });
              }
              // 调用查询方法
              this.getClick(this.rowww);
            })
      }).catch(() => {

      });
    }
  },
  mounted() {
    this.getSystems();
    this.getemp();
    this.getparent();
  }
}

</script>

<style scoped>

</style>